Thread: Why don't toy use ( and ) instead of { and }?

  1. #16
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Dave_Sinkula
    TriKri: I am still wondering whether you are a troll or not. Why don't you provide some samples of what you are asking instead of asking vague questions and preying on the replies?
    I can't think of a particular good example, but say you write a swap macro
    Code:
    #define  SWAP_NUM(x, y)  ( \
        (x) += (y),            \
        (y) =  (x)-(y),        \
        (x) -= (y)             \
    )
    You will get a return value if you use parenthesis, namely the new value of x. (or is it?) That has to become handy in some situation, I know it!

  2. #17
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    That's using the comma operator which performs each expression and the value of the last expression evaluated is used as the expression's value. That entire thing is an expression which therefore may be stuffed into (), if however you wanted a common statement like while (which probably is going to use {}) you can see you will get an error.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by TriKri
    I can't think of a particular good example, but say you write a swap macro
    Code:
    #define  SWAP_NUM(x, y)  ( \
        (x) += (y),            \
        (y) =  (x)-(y),        \
        (x) -= (y)             \
    )
    You will get a return value if you use parenthesis, namely the new value of x. (or is it?) That has to become handy in some situation, I know it!
    No, that isn't a particularly good example, since that is one of the forms of swapping to avoid. And no, you don't really "get a return value". Run it through the precompiler and see what madness you have actually written.

    Today's Total
    Troll: 3
    Learner: 0

    But I may not have the complete box score.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Dave_Sinkula
    Today's Total
    Troll: 3
    Learner: 0
    That's bad! (i guess) But I am only trying to learn the language, what you can do and what you can't. Besides, how do you run the precompiler? I am currently using DevCpp.

    And that kind of swap, is it bad because it does not leave the exact values when swaping float values? Consider the xor swap instead then.
    Last edited by TriKri; 08-01-2006 at 07:42 AM.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    how do you run the precompiler? I am currently using DevCpp.
    To stop after preprocessing the file, pass -E to GCC at the command line or in the compiler arguments:
    Code:
    C:\>cd "My Documents\programs\directory"
    
    C:\My Documents\programs\directory>g++ -E file.cpp > file_preprocessed.txt
    
    C:\My Documents\programs\directory>
    Then look at file_preprocessed.txt.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    All this time for a ridiculous thread.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    > Consider the xor swap instead then.
    As Shakespeare would say, this is going from Bard to Verse.
    http://c-faq.com/expr/xorswapexpr.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Bubba
    All this time for a ridiculous thread.
    Hey, I am just trying to understand the language and things you can do with it! Maybe I don't come up with the best questions, but can you please try to ignore the fact that they are ridiculous? I want to know the answer of the questions I ask, not that I shouldn't ask them, so please do not question my questionings.
    Last edited by TriKri; 08-01-2006 at 02:09 PM.

  9. #24
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Salem
    > Consider the xor swap instead then.
    As Shakespeare would say, this is going from Bard to Verse.
    http://c-faq.com/expr/xorswapexpr.html
    That is an incorrectly written xor swap. The real one doesn't produce any faulty values.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go on then, show us the "real one". Then explain how it's different.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by quzah
    Go on then, show us the "real one". Then explain how it's different.


    Quzah.
    Don't be so cocky, it says "It attempts to modify the variable a twice between sequence points, so its behavior is undefined". The real one doesn't and hence the result isn't undefined.

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, so you're saying you can't write the "correct one", right? Yeah, I thought so. Shut the ........ up, get off the forum and stop wasting our time.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    You can leave if you want.

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In other words, like I said, you can't write the "real one", because you're just making ........ up again. Have a nice ban.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    Obviously didn't read the rest of the c.l.c FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed